Skip to content

fix(crypto): Copy HMAC bytes into a new std::vector, to avoid a use-after-free - #342

Merged
andreiltd merged 2 commits into
bytecodealliance:mainfrom
VirtualTim:hmac-key-ownership
Oct 22, 2026
Merged

andreiltd merged 2 commits into
bytecodealliance:mainfrom
VirtualTim:hmac-key-ownership

Conversation

@VirtualTim

Copy link
Copy Markdown
Contributor

Fixes #341.

@VirtualTim

This comment was marked as resolved.

@andreiltd andreiltd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! One note here: the JS class is storing the key in the slot but it looks like it never release it, so vector is leaking memory.

JS::SetReservedSlot(instance, std::to_underlying(Slots::KeyData), JS::PrivateValue(data.release()->data()));

The class should use finalizable policy and drop the vector in finalize method:

class CryptoKey : public BuiltinImpl<CryptoKey, FinalizableClassPolicy>

@andreiltd

Copy link
Copy Markdown
Member

Tracked here: #343

@andreiltd
andreiltd merged commit d3d8125 into bytecodealliance:main Oct 22, 2026
7 checks passed
devin-ai-integration Bot added a commit to draco2003/StarlingMonkey that referenced this pull request Nov 3, 2026
The salt BufferSource was held as a borrowed span until after the
iterations and hash members were read. Those reads run user JS (getters,
valueOf), which can detach or mutate the backing buffer — e.g. via
transfer — leaving the derived bits computed from detached memory, the
same class of issue fixed for HMAC key material in bytecodealliance#342.

Copy the salt into a std::vector immediately after value_to_buffer,
before any further property reads. Adds a regression test whose
iterations getter detaches the salt buffer mid-normalization.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use-after-free when importing HMAC JWTs

2 participants